package com.project.controller.web;

import com.project.entity.Major;
import com.project.myBeans.ResponseBean;
import com.project.service.MajorService;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WheelChen on 2017/6/22.
 */
@Controller
@RequestMapping("/major")
public class MajorController extends BaseService {
    @Autowired
    MajorService majorService;

    /**
     * author: WheelChen
     * date: 2017-6-22
     * function: 根据系id查找专业
     *
     * @param departmentid 系id
     * @return
     */
    @RequestMapping(value = "/findBath/{departmentid}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findBatch(@PathVariable("departmentid") Integer departmentid) {
        ResponseBean responseBean = new ResponseBean();
        try {
            List<Major> majors =  majorService.findBatch(departmentid);;
            responseBean.setSuccess(true);
            responseBean.setData(majors);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * @api {post} /major/addOne 添加单个专业
     * @apiName addOne
     * @apiGroup Major
     * @apiVersion 0.0.1

     * @apiParam {json} major 专业
     *
     * @apiSuccess {boolean} success 是否成功 true - 成功 false - 不成功
     * @apiSuccess {String} message 提示信息
     *
     * @apiParamExample {json} Request-Example:
     *   {
     *      "name":"计算机（IT）",
     *      "universityid":12,
     *      "schoolid":11,
     *      "departmentid":6
     *   }
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "firstname": "John",
     *       "lastname": "Doe"
     *     }
     *
     *
     *
     * @apiErrorExample Error-Response: 程序异常
     *     HTTP/1.1 404 程序异常
     *     {
     *        "data": null,
     *        "flag": 0,
     *        "message": "程序异常",
     *        "success": false,
     *        "total": 0
     *     }
     *
     * @apiErrorExample Error-Response: 专业已存在
     *     HTTP/1.1 200 专业已存在
     *    {
     *      "data": null,
     *      "flag": 0,
     *      "message": "该专业已存在",
     *      "success": false,
     *      "total": 0
     *    }
    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 添加单个专业
     *
     * @param major
     * @return
     */
    public ResponseBean addOne(@RequestBody Major major) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (majorService.checkMajorIsExist(major)) {
                responseBean.setSuccess(false);
                responseBean.setMessage("该专业已存在");
            } else {
                majorService.addOne(major);
                responseBean.setSuccess(true);
                responseBean.setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

}
